package top.kaoshanji.p2ch06a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.p2ch06a.support.DebugExecutorService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 代码清单 p2ch06a-03：abort方法的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/17 16:35
 */
public class ConnectionExample02 {

    private static final Logger logger = LogManager.getLogger(ConnectionExample02.class);

    public static void main(String[] args) throws SQLException {
        logger.info("...代码清单 p2ch06a-03：abort方法的使用示例...");

        Connection connection = DriverManager.getConnection("jdbc:derby://localhost/p2-java7book");

        ThreadPoolExecutor executor = new DebugExecutorService(2, 10, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());

        connection.abort(executor);
        executor.shutdown();

        try {
            executor.awaitTermination(5, TimeUnit.MINUTES);
            System.out.println(executor.getCompletedTaskCount());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

}
